package com.sc.nft.dao;


import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.nft.entity.EcologyPoolDaoRankConf;
import com.sc.nft.enums.EcologyPoolDaoRankConfShowStatusEnum;
import com.sc.nft.enums.EcologyPoolDaoRankTypeEnum;
import com.sc.nft.mapper.EcologyPoolDaoRankConfMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.joda.time.DateTime;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

/**
 * @author PC
 * @description 针对表【sc_ecology_pool_dao_rank_conf(生态池-品牌榜配置)】的数据库操作Dao
 * @createDate 2024-07-16 19:05:30
 */
@Slf4j
@Repository
public class EcologyPoolDaoRankConfDao extends ServiceImpl<EcologyPoolDaoRankConfMapper, EcologyPoolDaoRankConf> {

    /**
     * 获取最后的榜单结束时间
     *
     * @param ecologyPoolDaoRankTypeEnum 榜单类型
     * @return EcologyPoolDaoRankConf
     */
    public EcologyPoolDaoRankConf getRankConfLastTime(EcologyPoolDaoRankTypeEnum ecologyPoolDaoRankTypeEnum) {
        LambdaQueryWrapper<EcologyPoolDaoRankConf> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(EcologyPoolDaoRankConf::getRankType, ecologyPoolDaoRankTypeEnum);
        queryWrapper.orderByDesc(EcologyPoolDaoRankConf::getCreateTime);
        queryWrapper.last("limit 1");
        return getOne(queryWrapper, false);
    }

    public List<EcologyPoolDaoRankConf> getActiveRankings() {
        LambdaQueryWrapper<EcologyPoolDaoRankConf> wrapper = Wrappers.lambdaQuery();
        // 时间在开始时间和节数时间之内的数据
        wrapper.le(EcologyPoolDaoRankConf::getStartTime, DateUtil.date());
        wrapper.gt(EcologyPoolDaoRankConf::getEndTime, DateUtil.date());
        wrapper.eq(EcologyPoolDaoRankConf::getStatus, Boolean.TRUE);
        return list(wrapper);
    }


    /**
     * 获取最新开启的配置
     *
     * @return
     */
    public EcologyPoolDaoRankConf getLastActive() {
        LambdaQueryWrapper<EcologyPoolDaoRankConf> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(EcologyPoolDaoRankConf::getStatus, Boolean.TRUE);
        wrapper.orderByDesc(EcologyPoolDaoRankConf::getStartTime);
        wrapper.last("limit 1");
        return baseMapper.selectOne(wrapper);
    }

    public Page<EcologyPoolDaoRankConf> daoRankPage(int pageNo, int pageSize, String rankName) {
        LambdaQueryWrapper<EcologyPoolDaoRankConf> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(rankName), EcologyPoolDaoRankConf::getRankName, rankName);
        wrapper.orderByDesc(EcologyPoolDaoRankConf::getId);
        return page(new Page<>(pageNo, pageSize), wrapper);
    }

    /**
     * 历史榜单
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    public Page<EcologyPoolDaoRankConf> historyDaoRankPage(int pageNo, int pageSize) {
        LambdaQueryWrapper<EcologyPoolDaoRankConf> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EcologyPoolDaoRankConf::getStatus, Boolean.TRUE);
        wrapper.lt(EcologyPoolDaoRankConf::getEndTime, new Date());
        wrapper.orderByDesc(EcologyPoolDaoRankConf::getStartTime);
        return page(new Page<>(pageNo, pageSize), wrapper);
    }

}


